//
//  PersonChangePhoneView.m
//  Inforcollection
//
//  Created by macbook on 16/1/12.
//  Copyright © 2016年 oceansoft.liw. All rights reserved.
//

#import "PersonChangePhoneView.h"
#define userID [UserData shareUserData].userModel.account_id


@implementation PersonChangePhoneView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self creatAllViews];
        
    }
    return self;
}
- (void)creatAllViews
{
    array = @[@"旧手机号码",@"新手机号码",@"验证码"];
    arr = [array subarrayWithRange:NSMakeRange(0, 2)];
    [self creatTableView];

}

- (void)creatTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.scrollEnabled = NO;
    [[UITableViewHeaderFooterView appearance] setTintColor:KTableViewBackColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.backgroundColor = KTableViewBackColor;
    [self addSubview:_tableView];
    
}

#pragma mark -UITableView  DataSource & Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 2;
    }else if (section == 1){
        return 1;
    }else{
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PersonInfoTableViewCell *cell = [PersonInfoTableViewCell changeInfoWithTableView:tableView];

    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.nameLabel.text = arr[indexPath.row];
            _oldPhoneNumberField = cell.infoTextField;
            [_oldPhoneNumberField addTarget:self action:@selector(TextFieldDidChanges:) forControlEvents:UIControlEventEditingChanged];
        }else if (indexPath.row == 1){
            PhoneYZMTableViewCell *cell = [PhoneYZMTableViewCell phoneYZMGetWithTableView:tableView];
            cell.nameLabel.text = arr[indexPath.row];
            _newPhoneNumberField = cell.YZMTextField;
            [_newPhoneNumberField addTarget:self action:@selector(TextFieldDidChanges:) forControlEvents:UIControlEventEditingChanged];
            cell.YZMLabel.tag = UserTag + 1;
            [cell touchLabelGetYZM1];
            return cell;
        }
    }else if (indexPath.section == 1){
        cell.nameLabel.text = [array lastObject];
        _YZM2Field = cell.infoTextField;
        
        [_YZM2Field addTarget:self action:@selector(TextFieldDidChanges:) forControlEvents:UIControlEventEditingChanged];

        
    }
    return cell;
}
#pragma mark -UITableView 的 HeaderView与FooterView
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 5;
    }else if (section == 1){
        return 10;
    }else{
        return 40;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.00000001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        UIView *headerview = [[UIView alloc] init];
        headerview.backgroundColor = [UIColor colorWithRed:232.0/255.0 green:232.0/255.0 blue:232.0/255.0 alpha:1.0];
        
        UIButton *saveBtn = [UIButton buttonWithType:0];
        saveBtn.frame = CGRectMake(10, 10, tableView.frame.size.width-20, 40);
        saveBtn.backgroundColor = [UIColor colorWithRed:93/255.0f green:129/255.0f blue:221/255.0f alpha:1];
        saveBtn.layer.cornerRadius=5.0f;
        
        [saveBtn setTitle:@"保存修改" forState:UIControlStateNormal];
        [saveBtn addTarget:self action:@selector(saveChangeInPage3) forControlEvents:UIControlEventTouchUpInside];
        
        saveBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
        [headerview addSubview:saveBtn];
        return headerview;

    }
        return nil;

}
- (void)saveChangeInPage3
{
    if (![self checkTel:_oldPhoneNumberField.text]) {
        
        return;
    }
    if (![[_oldPhoneNumberField text] isEqualToString:[UserData shareUserData].userModel.phone]) {
        [self makeToast:@"请输入原绑定手机"];
        return;
    }
    if (![self checkTel:_newPhoneNumberField.text]) {
        return;
    }
    if ([[_oldPhoneNumberField text] isEqualToString:[_newPhoneNumberField text]]) {
        [self makeToast:@"两次输入的相同"];
        return;
    }
    if (_YZM2Field.text.length == 0) {
        [self makeToast:@"验证码不能为空"];
        return;
    }
//    NSDictionary *postDic = @{@"account_id":userID,@"password":@"",@"oldMobile":_oldPhoneNumberField.text,@"newMobile":_newPhoneNumberField.text};
   }
#pragma mark -UITextField输入的约束
- (void)TextFieldDidChanges:(UITextField *)textField
{
    if (textField == _newPhoneNumberField){
        if (textField.text.length > 11) {
            textField.text = [textField.text substringToIndex:11];
        }
    }else if (textField == _oldPhoneNumberField){
        if (textField.text.length > 11) {
            textField.text = [textField.text substringToIndex:11];
        }
    }
    else if (textField == _YZM2Field)
    {
        if (textField.text.length > 6) {
            textField.text = [textField.text substringToIndex:6];
        }
    }
}


@end
